<?php
class Product extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->layout->setLayout('admin');
		$auth = $this->session->userdata('auth');
		if(!$auth)
		{
			redirect('/auth/login');
		}
	}
	function index()
	{
		$this->load->library('pagination');
		//paging
		$config['base_url'] =base_url()."index.php/product/index";
		$config['total_rows'] = $this->db->count_all('product');
		$config['per_page'] = '10';
		$config['uri_segment'] = 3;
		$this->pagination->initialize($config);
		//-------------
		$page=$this->uri->segment(3);
		$product=$this->product_model->getAll($page,$config['per_page']);
		$data['product']=$product;
		$this->layout->view('listproduct',$data);
	}
	function delete()
	{
		$id=$this->input->post('hidID');
		$arrId=$this->input->post('chkid');
		if($id)
			$this->product_model->delete($id);
		elseif($arrId)
		{
			foreach ($arrId as $id)
				$this->product_model->delete($id);
		}
		redirect('product');
	}
	function add()
	{
		$data['value'] = array(	'id'=>'',
								'name'=>'',
								'summary'=>'',
								'content'=>'',
								'image'=>''
							);
		$data['action']='insert';
		$data['error']='';
		$this->layout->view('addproduct',$data);
	}
	function insert()
	{
		//var_dump($this->input->post());die;
		
		//upload file
		$config['upload_path'] = './upload/product';
		$config['allowed_types'] = 'gif|jpg|png|jpeg';
		$config['max_size']	= '1024';
		$config['max_width']  = '1024';
		$config['max_height']  = '768';
		$this->load->library('upload', $config);
		$this->upload->initialize($config);
		//-------------------
		//var_dump($this->upload->data());die;
		if (!$this->upload->do_upload())
		{
			$data['error'] = array('error' => $this->upload->display_errors());
			$data['value']=array(	'id'=>'',
								'name'=>'',
								'summary'=>'',
								'content'=>'',
								'image'=>'',
								);
			$data['action']='insert';
			$this->load->view('addproduct', $data);
		
		}

			$arrProduct=$this->input->post();
			$img = $this->upload->data();
			$arr=array('id'=>null,
						'name'=>$arrProduct['txtname'],
						'summary'=>$arrProduct['summary'],
						'content'=>$arrProduct['content'],
						'image'=>$img['file_name']
						);
			$this->product_model->addproduct($arr);
			redirect('product');
	}
	function update()
	{
		$id=$this->uri->segment(3);
		$product=$this->product_model->getProduct($id);
		//var_dump($product);die;
		$data['value']=array(	'id'=>$id,
								'name'=>$product[0]->name,
								'summary'=>$product[0]->summary,
								'content'=>$product[0]->content,
								'image'=>$product[0]->image,
								);
		//var_dump($data['value']);die;
		$data['action']='submitupdate';
		$data['error']='';
		$this->layout->view('addproduct',$data);
	}
function submitupdate()
	{
		$config['upload_path'] = './upload/product';
		$config['allowed_types'] = 'gif|jpg|png|jpeg';
		$config['max_size']	= '1024';
		$config['max_width']  = '1024';
		$config['max_height']  = '768';
		$this->load->library('upload', $config);
		$this->upload->initialize($config);
		//-------------------
		//var_dump($this->upload->data());die;
		$id=$this->input->post('hidID');
		$data['file_name']='';
		if (!$this->upload->do_upload())
		{
			//var_dump($this->upload->data());die;
			$error = array('error' => $this->upload->display_errors());
			$this->load->view('addproduct', $error);
		}
		elseif($this->upload->do_upload())
		{
			//var_dump($this->upload->data());die;
			$data = $this->upload->data();
			$arrProduct=$this->input->post();
			$arr=array('id'=>$id,
						'name'=>$arrProduct['txtname'],
						'summary'=>$arrProduct['summary'],
						'content'=>$data['content'],
						'image'=>$data['file_name']
						);
			$this->product_model->update($arr);
			redirect('product');
		}
			$arrProduct=$this->input->post();
			$arr=array('id'=>$id,
						'name'=>$arrProduct['txtname'],
						'summary'=>$arrProduct['summary'],
						'content'=>$arrProduct['content'],
						);
		$this->product_model->update($arr);
		redirect('product');
	}
}